package com.jxtti.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jxtti.dao.DistrictDao;
import com.jxtti.model.District;

/**
 * 区域信息相关
 * 
 * @author minshui
 *
 */
@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class DistrictService {

	@Autowired
	private DistrictDao districtDao;
	
	
	public List<District> queryByList(Map<String, String> data){
		return districtDao.queryByList(data);
	}

	public List<District> queryByListAuth(Map<String, Object> data){
		return districtDao.queryByListAuth(data);
	}
	
	public District queryByAuthId(Map<String, Object> data)
	{
		return this.districtDao.queryByAuthId(data);
	}
	
	/**
	 * ztree控件获取区域树
	 * 
	 * @param data
	 * @return
	 */
	public List<Map<String, String>> findByPidZtreeCheck(
			Map<String, String> data) {
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();

		if (null == data.get("pid")) {
			data.put("pid", District.ROOT_ID.toString());
		}
		List<District> list = districtDao.findByPidZtreeCheck(data);
		for (District district : list) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", district.getId().toString());
			map.put("name", district.getName());
			map.put("parentId", district.getParentId().toString());
			if (null != data.get("level") && "0".equals(data.get("level"))) {
				map.put("isParent", "false");
			} else {
				map.put("isParent", "true");
			}
			result.add(map);
		}

		return result;
	}

	
	@Autowired
	private ArticleListService articleListService;
	
	/**
	 * 返回 z-tree 区域 站点 树结构
	 * 
	 * @param data
	 * @return
	 */
	public List<District> getzTreeNodes(Map<String, String> data) {			
		List<District> resultList=new ArrayList<District>();	
		
		if (null == data.get("pid")) {
			data.put("pid", "3607");
			//追加上赣州
			Map<String, String> gzdata =new HashMap<String, String>();	
			gzdata.put("id","3607");
			List<District> gzlist=districtDao.queryByList(gzdata);	
			gzlist.get(0).setType((byte) 2);
			resultList.add(gzlist.get(0)); 
		}
				
		List<District> list=new ArrayList<District>();
		
		if (data.get("level")!=null&&data.get("level").equals("0")) { // 第一级
			data.put("type","0");//只查找 部门
			list=districtDao.queryByListNoArea(data);	
						
			//去文章表 查询 该 机构 是否有文章
//			Map<String, Object> articledata =new HashMap<String, Object>();
//			articledata.put("is_deleted", "0");
//			articledata.put("fk_orgId", data.get("parent")); //组织机构			
//			articledata.put("status", data.get("articleStatus")); //文章状态>0				
//			List<ArticleList>  tArticleList= articleListService.queryAll(articledata);
//			if (null != tArticleList && 0 != tArticleList.size()) { //判断是否有数据
//				list=districtDao.queryByList(data); //出现科室
//			}			
			
		}else{ //根目录 直接查询下一级		
			data.put("type","1");//只查找地区 
			list=districtDao.queryByListNoArea(data);  //如果要出现 县 则将 	queryByListNoArea--->>queryByList			
		}		
		resultList.addAll(list);	
		
		return resultList;
	}
	
	
	/**
	 * 返回easyui数结构的组织架构
	 * 
	 * @param data
	 * @return
	 */
	public List<Map<String, Object>> findByPidAll(Map<String, String> data) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

		String pid = (null == data.get("pid")) ? "3607" : data.get("pid");
		List<District> list = districtDao.findByPidAll(pid);
		if (null != list && 0 != list.size()) {
			for (int i = 0; i < list.size(); i++) {
				Map<String, Object> node = new HashMap<String, Object>();
				node.put("id", list.get(i).getId());
				node.put("text", list.get(i).getName());
				if (1 == list.get(i).getType()) {
					// 区域节点，需要展开下级
					List<Map<String, Object>> childResult = new ArrayList<Map<String, Object>>();
					List<District> childList = districtDao.findByPidAll(list.get(i).getId().toString());
					if (null != childList && 0 != childList.size()) {
						for (int j = 0; j < childList.size(); j++) {
							Map<String, Object> childNode = new HashMap<String, Object>();
							childNode.put("id", childList.get(j).getId());
							childNode.put("text", childList.get(j).getName());
							childResult.add(childNode);
						}
					}
					node.put("children", childResult);
					if (null!=data.get("isCounty")&&data.get("isCounty").equals("yes")) { //只加载县级
						result.add(node);
					}					
				}
				
				if (null==data.get("isCounty")) { //单位和县城
					result.add(node);
				}
				
			}
		}

		return result;
	}

	public List<Map<String, String>> getWholeTree(Map<String, String> data) {
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();

		if (null == data.get("pid")) {
			data.put("pid", District.ROOT_ID.toString());
		}
		List<District> list = districtDao.getWholeTree(data);
		for (District district : list) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", district.getId().toString());
			map.put("name", district.getName());
			map.put("parentId", district.getParentId().toString());
			if (1 == district.getType()) {
				map.put("isParent", "true");
			} else {
				map.put("isParent", "false");
			}
			result.add(map);
		}

		return result;
	}

	/**
	 * 更新一条组织架构数据
	 * 
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public int update(Map<String, String> data) throws Exception {
		int result = districtDao.update(data);
/*
		if (result > 0) {
			// 本项目操作成功后续调用WS接口同步数据
			// 先获取完整的组织架构名称
			String fullName = districtDao.getFullNameById(data);
			JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
			// url为调用webService的wsdl地址
			Client client = dcf.createClient(Constants.WSDL_URL);
			// namespace是命名空间，methodName是方法名
			QName name = new QName(Constants.NAMESPACE_URI, "EditOrg");
			Object[] objects = client.invoke(name, data.get("name"), data.get("id"),data.get("preId"), "");
			if (null != objects) {
				JSONObject jsonObject = JSONObject.fromObject(objects[0]);
				boolean wsRet = jsonObject.getBoolean("flag");
				if (wsRet) {

				} else {
					throw new Exception("web service服务调用失败，数据回滚:"
							+ jsonObject.getString("info"));
				}
			} else {
				throw new Exception("web service服务调用失败，数据回滚");
			}
		}
*/
		return result;
	}

	/**
	 * 增加一条组织架构数据
	 * 
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public int add(Map<String, String> data) throws Exception {
		int result = districtDao.add(data);
		/*
		if (result > 0) {
			// 本项目操作成功后续调用WS接口同步数据
			// 先获取完整的组织架构名称
			String fullName = districtDao.getFullNameById(data);
			JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory
					.newInstance();
			// url为调用webService的wsdl地址
			Client client = dcf.createClient(Constants.WSDL_URL);
			// namespace是命名空间，methodName是方法名
			QName name = new QName(Constants.NAMESPACE_URI, "AddOrg");
			Object[] objects = client
					.invoke(name, data.get("name"), data.get("id"), "");
			if (null != objects) {
				JSONObject jsonObject = JSONObject.fromObject(objects[0]);
				boolean wsRet = jsonObject.getBoolean("flag");
				if (wsRet) {

				} else {
					throw new Exception("web service服务调用失败，数据回滚:"
							+ jsonObject.getString("info"));
				}
			} else {
				throw new Exception("web service服务调用失败，数据回滚");
			}
		}
		 */
		return result;
	}

	/**
	 * 删除一条组织架构数据
	 * 
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public int delete(Map<String, String> data) throws Exception {
		int result = districtDao.delete(data);
		/*
		if (result > 0) {
			// 本项目操作成功后续调用WS接口同步数据
			JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory
					.newInstance();
			// url为调用webService的wsdl地址
			Client client = dcf.createClient(Constants.WSDL_URL);
			// namespace是命名空间，methodName是方法名
			QName name = new QName(Constants.NAMESPACE_URI, "DelOrg");
			Object[] objects = client.invoke(name, data.get("id"));
			if (null != objects) {
				JSONObject jsonObject = JSONObject.fromObject(objects[0]);
				boolean wsRet = jsonObject.getBoolean("flag");
				if (wsRet) {

				} else {
					throw new Exception("web service服务调用失败，数据回滚:"
							+ jsonObject.getString("info"));
				}
			} else {
				throw new Exception("web service服务调用失败，数据回滚");
			}
		}
		 */
		return result;
	}
	
	public  String getFullNameById(Map<String, String> data){
		return districtDao.getFullNameById(data);
	}
	
}
